<template>
	<view>
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<view class="search-box">
				<view style="display: flex; padding: 15rpx;">
					<!-- #ifdef APP-PLUS -->
					<u-search placeholder="(手机)传票号" search-icon="scan" v-model="dataForm.citationNumber" height="72"
						:show-action="false" @focus="focusCitationNumber('APP')">
					</u-search>
					<u-search placeholder="(PDA)传票号" search-icon="scan" v-model="dataForm.citationNumber" height="72"
						:show-action="false" @focus="focusCitationNumber('PDA')">
					</u-search>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<u-search placeholder="请扫传票号" search-icon="scan" :show-action="false"
						v-model="dataForm.citationNumber" height="72" @search="focusCitationNumber('H5')">
					</u-search>
					<!-- #endif -->
					<u-search placeholder="工艺" v-model="citationName" height="72" disabled :show-action="false">
					</u-search>
				</view>
			</view>
			<view class="search-box">
				<view style="display: flex; ">
					<u-form-item label="实际完工日">
						<u-input v-model="dataForm.ub031" type="select" border placeholder="日期"
							@click="showub031 = true" />
						<lys-date v-if="showub031" type="1" @cancleTime="showub031 = false"
							@submitTime="submitub031"></lys-date>
					</u-form-item>
					<u-form-item label="投入数量">
						<u-input v-model="dataForm.ub010" type="number" border placeholder="投入数量"
							@change="change"></u-input>
					</u-form-item>
				</view>
			</view>
			<view style="margin: 20rpx;">
				<u-form-item label="机台" label-width="80">
					<u-input v-model="dataForm.machine" type="select" border placeholder="请选择机台"
						@click="selectShow = true" />
				</u-form-item>
				<u-form-item label="备注" label-width="80">
					<u-input v-model="dataForm.ub034" border placeholder="请输入备注"></u-input>
				</u-form-item>
			</view>
			<view style="margin: 20rpx;display: flex;">
				<u-form-item label="人数" label-width="80">
					<u-input v-model="dataForm.uj014" type="number" border placeholder="请输入人数" />
				</u-form-item>
				<u-form-item label="工时" label-width="80">
					<u-input v-model="dataForm.uj015" border placeholder="请输入工时"></u-input>
				</u-form-item>
			</view>
			<view class="search-box">
				<view style="display: flex; ">
					<u-form-item label="总工时" label-width="180">
						<u-input v-model="workingHours" disabled placeholder=""></u-input>
					</u-form-item>
					<u-form-item label="损耗">
						<u-input v-model="dataForm.ub048" disabled placeholder=""></u-input>
					</u-form-item>
				</view>
			</view>
			<view class="search-box">
				<view style="display: flex; ">
					<u-form-item label="本批总数量">
						<u-input v-model="dataForm.alreadyUb010" type="number" disabled placeholder=""></u-input>
					</u-form-item>
					<u-form-item label="待投入数量">
						<u-input v-model="dataForm.toBeInvested" type="number" disabled placeholder=""></u-input>
					</u-form-item>
				</view>
			</view>
			<view class="search-box">
				<view style="display: flex; ">
					<u-form-item label="已产出数量">
						<u-input v-model="dataForm.alreadyUb011" type="number" disabled placeholder=""></u-input>
					</u-form-item>
					<u-form-item label="已报废数量">
						<u-input v-model="dataForm.alreadyUb012" type="number" disabled placeholder=""></u-input>
					</u-form-item>
				</view>
			</view>
			<view class="search-box">
				<view style="display: flex; ">
					<u-form-item label="不良数量">
						<u-input v-model="dataForm.ub012" type="number" disabled placeholder=""></u-input>
					</u-form-item>
					<u-form-item label="产出数量">
						<u-input v-model="dataForm.ub011" type="number" disabled placeholder=""></u-input>
					</u-form-item>
				</view>
			</view>
		</u-form>
		<u-subsection :list="lsubsectionList" :current="current" @change="sectionChange"></u-subsection>
		<view v-show="current===0">
			<scroll-view scroll-y="true" style="height: 300rpx;">
				<uni-table border stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th align="center">序号</uni-th>
						<uni-th align="center">不良名称</uni-th>
						<uni-th align="center">不良次数</uni-th>
						<uni-th width="204" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,i) in  badCodeList" :key="i">
						<uni-td align="center">{{i+1}}</uni-td>
						<uni-td align="center">
							<u-input v-model="item.uh008" border />
						</uni-td>
						<uni-td align="center">
							<u-input v-model="item.uh009" border />
						</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button class="uni-button" size="mini" type="warn" @click="handleDel(i)">删除</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
			</scroll-view>
		</view>
		<view v-show="current===1">
			<scroll-view scroll-y="true" style="height: 300rpx;">
				<uni-table border stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th align="center">序号</uni-th>
						<uni-th align="center">品号</uni-th>
						<uni-th align="center">工艺参数名称</uni-th>
						<uni-th align="center">工艺参数参考值</uni-th>
						<uni-th align="center">工艺参数值</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,i) in  processList" :key="i">
						<uni-td align="center">{{i+1}}</uni-td>
						<uni-td align="center">{{item.wa002}}</uni-td>
						<uni-td align="center">{{item.wa004}}</uni-td>
						<uni-td align="center">{{item.wa008}}</uni-td>
						<uni-td align="center">
							<u-input v-model="item.craftParamValue" border />
						</uni-td>
					</uni-tr>
				</uni-table>
			</scroll-view>
		</view>
		<view style=" height: 100rpx "></view>
		<view class="flowBefore-actions">
			<u-button class="buttom-btn" type="primary" :ripple="true" plain @click.stop="handleAdd"
				v-show="current===0">
				<u-icon label="新增" size="40" name="plus"></u-icon>
			</u-button>
			<u-button class="buttom-btn" type="primary" @click.stop="submit">
				提交
			</u-button>
		</view>
		<u-modal v-model="comShow" :show-cancel-button="true" :show-confirm-button="false" title=''>
			<u-form :errorType="['toast']" label-position="left" label-align="left">
				<u-form-item label="传票号">
					<u-input v-model="comNumber" placeholder="请扫描传票号" :focus="true"></u-input>
				</u-form-item>
			</u-form>
		</u-modal>
		<u-toast ref="uToast" />
		<u-select v-model="selectShow" :list="processOptions" @confirm="selectConfirm"></u-select>
	</view>
</template>

<script>
	import request from "@/utils/request";
	import {
		GetCmsmxList,
	} from '@/api/common'
	export default {
		data() {
			return {
				current: 0,
				workingHours: 0,
				comShow: false,
				showub031: false,
				selectShow: false,
				dataForm: {
					craftNumber: null,
					ub012: 0,
					citationNumber: undefined,
					alreadyUb010: null,
					alreadyUb011: null,
					alreadyUb012: null,
					uj014: 0, //人工
					uj015: 0, //工时
				},
				//不良原因列表
				badCodeList: [],
				// 工艺列表
				processList: [],
				// 机台列表
				processOptions: [],
				lsubsectionList: [{
						name: '不良'
					},
					{
						name: '工艺'
					}
				],
				comNumber: null,

			}
		},
		watch: {
			badCodeList: {
				handler(newVal, oldVal) {
					let scrapQuantity = newVal.reduce(
						(sum, e) => sum + Number(e.uh009 || 0),
						0
					);
					//报废数量
					this.dataForm.ub012 = scrapQuantity;
					//投入数量
					let ub010 = parseFloat(this.dataForm.ub010 || 0);
					//损耗
					let ub048 = parseFloat(this.dataForm.ub048 || 0);
					//产出数量=投入数量-报废数量
					this.dataForm.ub011 = parseFloat(ub010 - scrapQuantity - ub048);
					this.change()
				},
				deep: true
			},
			"comNumber": {
				handler(newVal, oldVal) {
					if (newVal) {
						setTimeout(() => {
							this.dataForm.citationNumber = newVal;
							this.keyupHandle()
							this.comShow = false
						}, 500)
					}
				},
				deep: true,
			},
			"dataForm.ub010": {
				handler(newVal, oldVal) {
					this.change()
				},
				deep: true,
			},
			"dataForm": {
				handler(newVal, oldVal) {
					this.getWorkingHours()
				},
				deep: true,
			},
		},
		onLoad(option) {
			this.dataForm.craftNumber = option.ub004;
			this.citationName = option.ub004Name;
			this.getCMSMXList()

		},
		onShow() {
			uni.$on('upbadCodeList', (data) => {
				console.log('data', data);
				this.badCodeList.push(...data)
			})
		},
		onUnload() {
			uni.$off('upbadCodeList')
		},
		methods: {
			change() {
				this.dataForm.ub011 = this.dataForm.ub010 - this.dataForm.ub012;
			},
			// 计算总工时
			getWorkingHours() {
				this.workingHours = this.dataForm.uj014 * this.dataForm.uj015
			},
			sectionChange(index) {
				this.current = index;
			},
			// 获取机台
			getCMSMXList() {
				GetCmsmxList(this.dataForm.craftNumber).then(res => {
					this.processOptions = res.data.map(item => {
						return {
							label: item.JoinStr,
							value: item.MW001,
						}
					});
				});
			},
			// 机台选择确认
			selectConfirm(e) {
				console.log('机台选择确认', e);
				this.dataForm.machine = e[0].label.replace(/\(.*?\)/g, '')

			},
			submitub031(time, type, timeInt) {
				let ub031 = time.split("-");
				this.dataForm.ub031 = ub031[0] + ub031[1] + ub031[2];
				this.showub031 = false;
			},
			handleDel(index) {
				this.badCodeList.splice(index, 1);
			},
			focusCitationNumber(type) {
				if (type === 'APP') {
					uni.scanCode({
						success: (res) => {
							this.dataForm.citationNumber = res.result;

							this.keyupHandle()
						}
					});
				} else if (type === 'PDA') {
					this.comNumber = null;
					this.comShow = true
				} else {
					this.keyupHandle()
				}
			},
			handleAdd() {
				uni.navigateTo({
					url: `/pages/workFlow/productionScan/com/badOver`
				});
			},
			//获取工艺列表
			getProcessParamList(ua006) {
				console.log('获取工艺列表==>craftNumber', this.dataForm.craftNumber);
				let query = {
					wa001: this.dataForm.craftNumber, //工艺编号
					wa002: ua006,
					currentPage: 1,
					pageSize: 5,
					sort: "desc",
					sidx: "",
				};
				console.log('工艺==>', query);
				request({
					url: `/api/SubDev/CmsmwA/getProcessParamList`,
					method: "GET",
					data: query,
				}).then((res) => {
					this.processList = res.data;
				});
			},
			//传票号扫码触发方法
			keyupHandle() {
				// this.dataForm.alreadyUb010 = 1000; //总投入数量
				// this.dataForm.alreadyUb011 = 500; //已产出数量
				// this.dataForm.alreadyUb012 = 200; //已报废数量
				// return
				console.log('传票号扫码触发方法==>craftNumber', this.dataForm.craftNumber);
				let query = {
					processNumber: this.dataForm.craftNumber, //工艺编号
					citationNumber: this.dataForm.citationNumber, //传票号
				};
				request({
						url: `/api/SubDev/Sfcub/getReportingForWork`,
						method: "get",
						data: query,
					})
					.then((res) => {
						if (res.code == 200) {
							this.dataForm = res.data;
							this.dataForm.alreadyUb010 = res.data.ub010; //总投入数量
							this.dataForm.alreadyUb011 = res.data.ub011; //已产出数量
							this.dataForm.alreadyUb012 = res.data.ub012; //已报废数量
							this.dataForm.toBeInvested = (
								this.dataForm.alreadyUb010 -
								this.dataForm.alreadyUb011 -
								this.dataForm.alreadyUb012
							).toFixed(3);
							this.dataForm.ub010 = this.dataForm.toBeInvested;
							this.dataForm.ub011 = this.dataForm.ub010;
							this.dataForm.ub012 = 0;
							this.dataForm.craftNumber = res.data.ub004;
							this.getProcessParamList(res.data.ua006);
						}
					})
			},
			//项目计划验证
			verify() {
				let isOk = true;
				for (let i = 0; i < this.badCodeList.length; i++) {
					const e = this.badCodeList[i];
					if (this.jnpf.isEmpty(e.ub205)) {
						return this.$refs.uToast.show({
							title: `传票号${ e.citationNumber}接收数量不能为空!`,
							type: "error",
						});
						isOk = false;
						break;
					}
				}
				return isOk;
			},
			// 提交
			submit() {
				let ub048 = parseFloat(this.dataForm.ub048 || 0);
				let ub011 = parseFloat(this.dataForm.ub011 || 0);
				let ub012 = parseFloat(this.dataForm.ub012 || 0);
				const ubSum = ub011 + ub012 + ub048
				if (this.dataForm.ub010 > ubSum) {
					return this.$refs.uToast.show({
						title: `投入数量不能超过工${ubSum}`,
						type: "error",
					});
				}
				if (!this.dataForm.machine) {
					return this.$refs.uToast.show({
						title: "请先选择机台",
						type: "error",
					});
				}
				var paramData = {
					...this.dataForm,
					cmsmwAList: this.processList, //工艺列表
					sfcuhList: this.badCodeList, //不良列表
					ua013: this.dataForm.citationNumber, //传票号
				};
				console.log('paramData', paramData);

				uni.showModal({
					title: '提示',
					content: '您确定要提交当前数据吗？',
					success: res => {
						if (res.confirm) return this.confirm(paramData)
					}
				})
			},
			//确 认
			confirm(paramData) {
				request({
						url: `/api/SubDev/Sfcub/updateEntity`,
						method: "PUT",
						data: paramData,
					})
					.then((res) => {
						uni.showToast({
							title: res.msg,
							complete: () => {
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							}
						})
					})
			},
		}
	}
</script>